'''
Author: xialang333@gmail.com
Date: 2024-07-11 15:19:52
LastEditTime: 2024-07-17 16:50:50
'''
from django.db import models
from django_apscheduler.models import DjangoJob

from utils.models import CoreModel, BaseModel, table_prefix, CoreIntModel


class Task(CoreModel):
    STATUS_LIST = (
        (0, "停用"),
        (1, "启用")
    )
    status = models.IntegerField(default=1, choices=STATUS_LIST, verbose_name="任务状态", help_text="任务状态")
    name = models.CharField(max_length=30, verbose_name="任务名称", help_text="任务名称")
    cron = models.CharField(max_length=255, verbose_name="cron表达式", help_text="cron表达式")
    job_id = models.OneToOneField(DjangoJob, db_constraint=False, on_delete=models.CASCADE,
                                  verbose_name="关联DjangoJob", help_text="关联DjangoJob", null=True, blank=True)
    job = models.CharField(max_length=255, verbose_name="执行的任务", help_text="执行的任务")

    class Meta:
        db_table = table_prefix + "crond_task"
        verbose_name = '定时任务表'
        verbose_name_plural = verbose_name
        ordering = ('create_datetime',)


class TaskResult(CoreIntModel):
    job_id = models.CharField(max_length=100, null=True, blank=True, verbose_name="任务id", help_text="任务id")
    status = models.CharField(max_length=100, null=True, blank=True, verbose_name="执行状态", help_text="执行状态")
    run_time = models.CharField(max_length=100, null=True, blank=True, help_text="执行时间", verbose_name="执行时间")
    result = models.JSONField(verbose_name="执行结果",null=True,blank=True)

    class Meta:
        db_table = table_prefix + "crond_task_result"
        verbose_name = '任务结果表'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)
    


class TaskMethod(CoreModel):
    name = models.CharField(max_length=30, verbose_name="脚本名称", help_text="脚本名称")
    language_type = models.CharField(max_length=30, verbose_name="脚本语言", help_text="脚本语言")
    exec_method = models.CharField(max_length=255, verbose_name="执行方式", help_text="执行方式",null=True,blank=True)
    exec_param = models.JSONField(verbose_name="执行参数", help_text="执行参数",null=True,blank=True)
    content = models.TextField(verbose_name="脚本内容", help_text="脚本内容")
    pre_exec_log = models.JSONField(verbose_name="执行结果",null=True,blank=True, help_text="执行结果")

    class Meta:
        db_table = table_prefix + "crond_script"
        verbose_name = '脚本管理表'
        verbose_name_plural = verbose_name
        ordering = ('create_datetime',)
